package d512a.TAKO;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.zxing.client.android.CaptureActivity;

import d512a.TAKO.helpers.DataBaseHelper;
import d512a.TAKO.helpers.DateHelper;

public class ProductInfoVitamins extends Activity {
	private DataBaseHelper foodDB;
	private TextView txtProductName;
	private TextView txtAvitaminValue;
	private TextView txtDvitaminValue;
	private TextView txtEvitaminValue;
	private TextView txtKvitaminValue;
	private TextView txtB1vitamin_thiaminValue;
	private TextView txtB2vitamin_riboflavinValue;
	private TextView txtNiacinValue;
	private TextView txtB6vitaminValue;
	private TextView txtPantothensyreValue;
	private TextView txtBiotinValue;
	private TextView txtFolatValue;
	private TextView txtB12vitaminValue;
	private TextView txtCvitaminValue;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_info_vitamins);

		handleDatabase();
		addWidgetReferences();
		setWidgetListeners();
		String foodId = getIntent().getExtras().getString("foodId");
		getProductName(foodId);
		getDetailedNutrients(foodId);
		String date = DateHelper.getDate();
		
		if (ProductTabWidgetActivity.fromScan == true) {
            String scanningsID = getIntent().getExtras().getString("scanningsID");
            
            foodDB.insertInRecommended(0, null, scanningsID, 7, date).close();
        } else {
            // Get lastly inserted _id in the Recommended table
            Cursor c = foodDB.getRecommendedPrimKey_id();
            c.moveToFirst();
            String recommend_idPrimKey = c.getString(0);
            c.close();
            foodDB.insertInRecommended(0, recommend_idPrimKey, null, 7, date);
        }
	}

	private void handleDatabase() {
		foodDB = new DataBaseHelper(this);

		try {
			foodDB.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		} finally {
			foodDB.close();
		}
	}

	private void addWidgetReferences() {
		txtProductName = (TextView) findViewById(R.id.txtProductName);
		txtAvitaminValue = (TextView) findViewById(R.id.txtAvitaminValue);
		txtDvitaminValue = (TextView) findViewById(R.id.txtDvitaminValue);
		txtEvitaminValue = (TextView) findViewById(R.id.txtEvitaminValue);
		txtKvitaminValue = (TextView) findViewById(R.id.txtKvitaminValue);
		txtB1vitamin_thiaminValue = (TextView) findViewById(R.id.txtB1vitamin_thiaminValue);
		txtB2vitamin_riboflavinValue = (TextView) findViewById(R.id.txtB2vitamin_riboflavinValue);
		txtNiacinValue = (TextView) findViewById(R.id.txtNiacinValue);
		txtB6vitaminValue = (TextView) findViewById(R.id.txtB6vitaminValue);
		txtPantothensyreValue = (TextView) findViewById(R.id.txtPantothensyreValue);
		txtBiotinValue = (TextView) findViewById(R.id.txtBiotinValue);
		txtFolatValue = (TextView) findViewById(R.id.txtFolatValue);
		txtB12vitaminValue = (TextView) findViewById(R.id.txtB12vitaminValue);
		txtCvitaminValue = (TextView) findViewById(R.id.txtCvitaminValue);
	}

	private void setWidgetListeners() {

	}

	private void getProductName(String foodId) {
		Cursor cursor = foodDB.getProductNameFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			txtProductName.setText(cursor.getString(0));
		}
	}

	private void getDetailedNutrients(String foodId) {
		Cursor cursor = foodDB.getVitaminsNutrientsFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				int compId = Integer.parseInt(cursor.getString(0));
			    switch(compId) {
			        case 14:
                        txtAvitaminValue.setText(cursor.getString(1) + " RE");
                        break;
			        case 23:
                        txtDvitaminValue.setText(cursor.getString(1) + " µg");
                        break;
			        case 26:
                        txtEvitaminValue.setText(cursor.getString(1) + " α-TE");
                        break;
			        case 35:
                        txtKvitaminValue.setText(cursor.getString(1) + " µg");
                        break;
			        case 36:
                        txtB1vitamin_thiaminValue.setText(cursor.getString(1) + " mg");
                        break;
			        case 37:
                        txtB2vitamin_riboflavinValue.setText(cursor.getString(1) + " mg");
                        break;
			        case 38:
                        txtNiacinValue.setText(cursor.getString(1) + " NE");
                        break;
			        case 41:
                        txtB6vitaminValue.setText(cursor.getString(1) + " mg");
                        break;
			        case 45:
			            txtPantothensyreValue.setText(cursor.getString(1) + " mg");
			            break;
			        case 46:
                        txtBiotinValue.setText(cursor.getString(1) + " µg");
                        break;
			        case 47:
                        txtFolatValue.setText(cursor.getString(1) + " µg");
                        break;
			        case 49:
                        txtB12vitaminValue.setText(cursor.getString(1) + " µg");
                        break;
			        case 50:
                        txtCvitaminValue.setText(cursor.getString(1) + " mg");
                        break;
                    default:
                        throw new Error("Fejl 40!");
			    }
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.product_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Intent i;
		switch (item.getItemId()) {
		case R.id.menu_item_scan:
			startBarcodeScanner();
			return true;
		case R.id.menu_item_home:
			i = new Intent(getApplicationContext(), HomescreenActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_shoppingBasket:
			i = new Intent(getApplicationContext(), BasketActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_settings:
			i = new Intent(getApplicationContext(), UserInfoActivity.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void startBarcodeScanner() {
		Intent i = new Intent(getApplicationContext(), CaptureActivity.class);
		startActivity(i);
	}
}
